package de.saar.penguin.give2.server;

import java.util.List;
import java.util.Set;
import java.util.TreeSet;
import java.util.Vector;

/**
 *
 * @author Boris Fersing
 */
public class History {
    //contains the id of the visited rooms
    private Vector<String> visitedRooms = new Vector<String>();
    private Set<String> seenObjects = new TreeSet();

    /**
     * Appends a room the the history if needed and updates the seenObjects set
     * @param id the room id
     * @param visibleObjects the visible objects
     */
    public void update(String id, List<String> visibleObjects) {
        if (visitedRooms.size() == 0 || !visitedRooms.lastElement().equalsIgnoreCase(id))
            visitedRooms.add(id);
        updateObjects(visibleObjects);
    }

    /**
     * Updates the objects only
     * @param visibleObjects The visible objects
     */
    public void updateObjects(List<String> visibleObjects) {
        seenObjects.addAll(visibleObjects);
    }

    /**
     * Checks if a room is the last visited room (the room before the current one)
     * @param id the room id
     * @return true if the given room is the last visited, false otherwise
     */
    public boolean isPrevious(String id){
        if (getPreviousRoom().equalsIgnoreCase(id))
            return true;
        return false;
    }

    /**
     * @return Id of the last room visited
     */
    public String getPreviousRoom(){
        if (visitedRooms.size() > 1)
            return visitedRooms.get(visitedRooms.size()-2);
        return "";
    }

    /**
     *
     * @return The list of the previously visited rooms.
     */
    public List<String> getPreviousRooms() {
        int end;
        if (visitedRooms.size() == 0) {
            end = 0;
        }
        else {
            end = visitedRooms.size() - 1;
        }

        return visitedRooms.subList(0, end);
    }

    /**
     *
     * @return The set of the objects the player has already seen.
     */
    public Set<String> getSeenObjects(){
        return seenObjects;
    }
}
